package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.anno.ToEasyService;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.vo.DishPageQueryVO;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    Page<DishPageQueryVO> pageList(DishPageQueryDTO dishPageQueryDTO);

    @ToEasyService
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into dish(name, category_id, price, image, description, status, create_time, update_time, create_user, update_user) " +
            "values (#{name},#{categoryId},#{price}," +
            "#{image},#{description},#{status},#{createTime},#{updateTime}," +
            "#{createUser},#{updateUser})")
    void save(Dish dish);

    void saveFlavors(List<DishFlavor> flavors);

    @Select("select d.id, d.name, d.category_id, d.price, d.image, d.description, d.status, d.update_time," +
            "c.name from dish d left join category c on d.category_id = c.id where d.id=#{id}")
    DishVO getById(Integer id);


    List<DishFlavor> getFlavorsById(Integer id);

    @Select("select id, name, category_id, price, image, description, status, create_time, " +
            "update_time, create_user, update_user from dish where category_id=#{categoryId}")
    List<Dish> getByCategoryId(String categoryId);

    @ToEasyService
    @Update("update dish set name=#{name},category_id=#{categoryId},price=#{price},image=#{image}," +
            "description=#{description},status=#{status},update_time=#{updateTime},update_user=#{updateUser} where id=#{id} ")
    void update(Dish dish);

    void delete(Integer[] ids);

    void deleteFlavors(Integer id);

    @Update("update dish set status=#{status} where id=#{id} ")
    void updateStatus(Integer status, Integer id);

    @Select("select count(status) from dish where status=#{status}")
    Integer countStatus(Integer status);

    @Select("select d.category_id,d.name,d.description,d.id,d.image,d.name,d.price,d.status,d.update_time " +
            "from dish d left join category c on d.category_id = c.id where d.category_id=#{categoryId}")
    List<DishVO> getForUserByCategoryId(@Param("categoryId") String categoryId);
}
